/* 
 * Copyright (c) 2022, CENTRIN.CIYUN.LTD. All rights reserved.
 */
package com.cory.admin.sys.controller.admin;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.cory.admin.sys.controller.AdminController;
import com.cory.boot.domain.bean.Result;
import com.cory.boot.domain.enumdef.EOperateType;
import com.cory.boot.sys.entity.SysDictCode;
import com.cory.boot.sys.entity.SysDictValue;
import com.cory.boot.sys.service.SysDictService;
import com.cory.utils.lang.StringUtils;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;

/**
 * @author xiongcong
 * @date 2022-01-13
 */
@Controller
@RequestMapping("admin/sys/dict")
public class SysDictController extends AdminController {
    @Autowired
    private SysDictService sysDictService;
    
    @RequestMapping("list")
    public String list(SysDictCode param, Model model) {
        param.setSysType(2);
        super.startPage();
        List<SysDictCode> list = sysDictService.dictCodeList(param);
        
        model.addAttribute("page", new PageInfo<>(list));
        model.addAttribute("param", param);
        return "admin/sys/dict/list";
    }
    
    @RequestMapping("form")
    public String form(String dictCode, Model model) {
        if (StringUtils.isNotBlank(dictCode)) {
            model.addAttribute("info", sysDictService.getDictCodeById(dictCode));
            model.addAttribute("operate", EOperateType.UPDATE.key);
        } else {
            model.addAttribute("operate", EOperateType.ADD.key);
        }
        return "admin/sys/dict/form";
    }
    
    @RequestMapping("save")
    @ResponseBody
    public Result<?> save(SysDictCode info, Integer operate) {
        
        if (operate == EOperateType.ADD.key) {
            if (sysDictService.getDictCodeById(info.getDictCode()) != null) {
                return Result.fail("字典已存在");
            }
            
            info.setSysType(2);
            sysDictService.saveDictCode(info);
        } else {
            sysDictService.updateDictCodeById(info);
        }
        
        return Result.success("保存成功");
    }
    
    @RequestMapping("delete")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result<?> delete(String id, String ids) {
        List<String> idList = id != null ? Lists.newArrayList(id) : 
            Arrays.asList(ids.split(",")).stream().filter(item -> StringUtils.isNotBlank(item))
            .collect(Collectors.toList());
        
        if (idList.size() > 0) {
            for (String i : idList) {
                sysDictService.deleteDictCodeById(i);
            }
        }
        
        return Result.success((idList.size() > 1 ? "批量" : "") + "删除成功");
    }
    
    @RequestMapping("value/list")
    public String value_list(SysDictValue param, Model model) {
        assertNotNull(param.getDictCode());
        super.startPage();
        List<SysDictValue> list = sysDictService.dictValueList(param);
        
        model.addAttribute("page", new PageInfo<>(list));
        model.addAttribute("param", param);
        return "admin/sys/dict/value.list";
    }
    
    @RequestMapping("value/form")
    public String value_form(String dictCode, String dictKey, Model model) {
        assertNotNull(dictCode);
        if (StringUtils.isNotBlank(dictKey)) {
            model.addAttribute("info", sysDictService.getDictValue(dictCode, dictKey));
            model.addAttribute("operate", 2);
        } else {
            model.addAttribute("operate", 1);
        }
        model.addAttribute("dict", sysDictService.getDictCodeById(dictCode));
        return "admin/sys/dict/value.form";
    }
    
    @RequestMapping("value/save")
    @ResponseBody
    public Result<?> value_save(SysDictValue info, Integer operate) {
        
        if (operate == 1) {
            if (sysDictService.getDictValue(info.getDictCode(), info.getDictKey()) != null) {
                return Result.fail("字典已存在");
            }
            
            sysDictService.saveDictValue(info);
        } else {
            sysDictService.updateDictValue(info);
        }
        
        return Result.success("保存成功");
    }
    
    @RequestMapping("value/delete")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result<?> value_delete(String dictCode, String id, String ids) {
        assertNotNull(dictCode);
        List<String> idList = id != null ? Lists.newArrayList(id) : 
            Arrays.asList(ids.split(",")).stream().filter(item -> StringUtils.isNotBlank(item))
            .collect(Collectors.toList());
        
        if (idList.size() > 0) {
            sysDictService.deleteDictValue(dictCode, idList);
        }
        
        return Result.success((idList.size() > 1 ? "批量" : "") + "删除成功");
    }
    
    @RequestMapping("value/updateState")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result<?> value_updateState(String dictCode, String id, String ids, Integer state) {
        assertNotNull(dictCode, state);
        List<String> idList = id != null ? Lists.newArrayList(id) : 
            Arrays.asList(ids.split(",")).stream().filter(item -> StringUtils.isNotBlank(item))
            .collect(Collectors.toList());
        
        if (idList.size() > 0) {
            for (String dictKey : idList) {
                sysDictService.updateDictValueState(dictCode, dictKey, state);
            }
        }
        
        return Result.success((idList.size() > 1 ? "批量" : "") + (state == 1 ? "启用" : "禁用") + "成功");
    }
    
    @RequestMapping("value/sort")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result<?> value_sort(String dictCode, @RequestBody JSONObject body) {
        List<SysDictValue> list = Lists.newArrayList();
        
        body.entrySet().forEach(i -> {
            String id = i.getKey().toString();
            int sort = Integer.valueOf(i.getValue().toString());
            
            SysDictValue info = new SysDictValue();
            info.setDictKey(id);
            info.setSort(sort);
            list.add(info);
        });
        
        if (list.size() > 0) {
            sysDictService.sortDictValue(dictCode, list);
        }
        
        return Result.success("保存成功");

    }
    
    @ResponseBody
    @RequestMapping("value/ajaxList")
    public Result<?> value_ajax_list(SysDictValue param) {
        assertNotNull(param.getDictCode());
        List<SysDictValue> list = sysDictService.dictValueList(param);
        
        return Result.success("ok", list);
    }
    
    @ResponseBody
    @RequestMapping("value/ajaxMap")
    public Result<?> value_ajax_map(SysDictValue param) {
        assertNotNull(param.getDictCode());
        List<SysDictValue> list = sysDictService.dictValueList(param);
        
        Map<String, Object> map = list.stream().collect(Collectors.toMap(i -> i.getDictKey(), i -> i.getDictValue()));
        return Result.success("ok", map);
    }
}
